<?php
/**
 * Created by PhpStorm.
 * User: wangjie
 * Date: 2018/10/26
 * Time: 14:26
 */
namespace api\controllers;

use Yii;
use common\services\ToolTimeService;

class TimeController extends BaseController
{
    /**
     * 时间戳转换
     * @return array
     */
    public function actionTimeStampHandle()
    {
        $unixTimestamp = trim($this->request->post('unix_timestamp', 0));
        $type = trim($this->request->post('type', 0));
        $result = ToolTimeService::timeStampHandle($unixTimestamp, $type);
        return $this->endReponse('0',$result);
    }

    /**
     * 时间戳转时间
     * @return array
     */
    public function actionDateHandle()
    {
        $date = trim($this->request->post('date', ''));
        $result = ToolTimeService::dateHandle($date);
        return $this->endReponse('0',$result);
    }

    /**
     * 时间戳之间的时间差，负数说明前者在后者之前。
     * @return array
     */
    public function actionGetTimeDifference()
    {
        $time1 = trim($this->request->post('time1', ''));
        $time2 = trim($this->request->post('time2', ''));
        $result = ToolTimeService::getTimeDifference($time1, $time2);
        return $this->endReponse('0',$result);
    }

    /**
     * 时间戳距离当前时间多少天
     * @return array
     */
    public function actionGetDayPassed()
    {
        $time = trim($this->request->post('time', ''));
        $result = ToolTimeService::getDayPassed($time);
        return $this->endReponse('0',$result);
    }

    /**
     * date 时间 xxxx-xx-xx
     * day  天数
     * type 1增加 2减少
     * @return array
     */
    public function actionGetDateAdd()
    {
        $date = trim($this->request->post('date', ''));
        $day = trim($this->request->post('day', ''));
        $type = trim($this->request->post('type', '1'));
        $result = ToolTimeService::getDateAdd($date, $day , $type);
        return $this->endReponse('0',$result);
    }

    /**
     * 关于时间的描述
     * @return array
     */
    public function actionGetTimeDescribe()
    {
        $time = trim($this->request->post('unix_timestamp', time()));
        $result = ToolTimeService::getTimeToStringBetweenDateTimeWithToday($time);
        return $this->endReponse('0',$result);
    }

    /**
     * 将秒转换位 年月日、时分秒格式
     * @return array
     */
    public function actionSecondsToStr()
    {
        $second = trim($this->request->post('second', '0'));
        $result = ToolTimeService::secondsToStr($second);
        return $this->endReponse('0',$result);
    }
}